load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
load("//bazel:defs.bzl", "rust_ic_test")

package(default_visibility = ["//visibility:public"])

rust_library(
    name = "backend_lib",
    srcs = glob(["src/**/*.rs"]),
    crate_features = select({
        "//bazel:fuzzing_code_enabled": ["fuzzing_code"],
        "//conditions:default": [],
    }),
    crate_name = "ic_canister_sandbox_backend_lib",
    version = "0.9.0",
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/embedders",
        "//rs/interfaces",
        "//rs/interfaces/state_manager",
        "//rs/monitoring/logger",
        "//rs/monitoring/metrics",
        "//rs/replicated_state",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "//rs/types/wasm_types",
        "@crate_index//:bincode",
        "@crate_index//:bytes",
        "@crate_index//:lazy_static",
        "@crate_index//:libc",
        "@crate_index//:nix",
        "@crate_index//:num-traits",
        "@crate_index//:once_cell",
        "@crate_index//:prometheus",
        "@crate_index//:rayon",
        "@crate_index//:regex",
        "@crate_index//:serde",
        "@crate_index//:serde_bytes",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:threadpool",
        "@crate_index//:which",
    ] + select({
        "//bazel:fuzzing_code_enabled": ["@crate_index//:object"],
        "//conditions:default": [],
    }),
)

rust_ic_test(
    name = "backend_lib_test",
    crate = ":backend_lib",
    data = [
        "//rs/universal_canister/impl:universal_canister.module",
        "//rs/universal_canister/impl:universal_canister.wasm.gz",
    ],
    env = {
        "UNIVERSAL_CANISTER_WASM_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.wasm.gz)",
        "UNIVERSAL_CANISTER_SERIALIZED_MODULE_PATH": "$(rootpath //rs/universal_canister/impl:universal_canister.module)",
    },
    deps = [
        # Keep sorted.
        "//packages/ic-error-types",
        "//rs/cycles_account_manager",
        "//rs/limits",
        "//rs/registry/subnet_type",
        "//rs/test_utilities",
        "//rs/test_utilities/execution_environment",
        "//rs/test_utilities/metrics",
        "//rs/test_utilities/types",
        "//rs/types/base_types",
        "//rs/types/wasm_types",
        "@crate_index//:mockall",
        "@crate_index//:rand",
        "@crate_index//:rstest",
        "@crate_index//:slog-async",
        "@crate_index//:slog-term",
        "@crate_index//:tempfile",
        "@crate_index//:wat",
    ],
)

cargo_build_script(
    name = "build_script",
    srcs = ["build.rs"],
    data = ["src/backtrace.c"],
    deps = ["@crate_index//:cc"],
)

rust_binary(
    name = "canister_sandbox",
    srcs = ["bin/canister_sandbox.rs"],
    crate_name = "canister_sandbox",
    version = "0.9.0",
    deps = [
        # Keep sorted.
        ":backend_lib",
        ":build_script",
    ],
)

# `sandbox_launcher` executable name referenced by code and other scripts.
# Therefore reserve that name for the rust binary.
rust_binary(
    name = "sandbox_launcher",
    srcs = ["bin/sandbox_launcher.rs"],
    deps = [":backend_lib"],
)

rust_binary(
    name = "test_controller",
    srcs = ["bin/test_controller.rs"],
    deps = [
        # Keep sorted.
        ":backend_lib",
        "@crate_index//:tempfile",
    ],
)

rust_binary(
    name = "test_sandbox",
    srcs = ["bin/test_sandbox.rs"],
    deps = [":backend_lib"],
)

rust_binary(
    name = "compiler_sandbox",
    srcs = ["bin/compiler.rs"],
    crate_name = "canister_sandbox",
    version = "0.9.0",
    deps = [":backend_lib"],
)
